import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
/**
 * La clase ToolHolder solo agrupa los datos referentes a las 
 * dimensiones de las herramientas en forma general.
 */
public class ToolHolder extends DataCategory
{
    public ToolHolder()
    {
    }
    public boolean initData(int id,int value)
    {
        return false;
    }
    public boolean initData(int id,long value)
    {
        if(id > 0x0F)
            switch(id)
            {
                case MAXIMUM_TOOL_DIAMETER:
                    maxToolDiameter = value;
                    return true;
                case MAXIMUM_TOOL_LENGTH:
                    maxToolLength = value;
                    return true;
                default:
                    return false;
            }
        else
            return false;
    }
    public Component getView(Id viewID)
    {
        return null;
    }
    public Component getEditor(Id editorID)
    {
        JPanel panel;
        JPanel panel_aux1;
        JPanel panel_aux2;
        switch(editorID)
        {
            case TOOL_HOLDER_DATA_EDITOR:
                panel = NewComp.getPanel(new GridLayout(2,2));
                panel_aux1 = new JPanel(new GridBagLayout());
                panel_aux2 = new JPanel(new GridBagLayout());
                JLabel diameterLabel = NewComp.getLabel("maxToolDiameter","titleLabelBFrame","black");
                JLabel lenghtLabel = NewComp.getLabel("maxToolLenght","titleLabelBFrame","black");
                diameterField = NewComp.getNumberTxtField("textFieldNumBFrame");
                diameterField.setValue(toMmOrIn(
                    PathPlanning.getMetricSystem(),
                    maxToolDiameter)
                );
                diameterField.getDocument().addDocumentListener(
                    new DocumentListener() 
                    {
                        public void insertUpdate(DocumentEvent event) 
                        {
                            if(changed.lastIndexOf(Id.MAXIMUM_TOOL_DIAMETER) == -1)
                                changed.add(Id.MAXIMUM_TOOL_DIAMETER);
                        }
                        public void removeUpdate(DocumentEvent event) 
                        {
                            if(changed.lastIndexOf(Id.MAXIMUM_TOOL_DIAMETER) == -1)
                                changed.add(Id.MAXIMUM_TOOL_DIAMETER);
                        }
                        public void changedUpdate(DocumentEvent event){}
                    }
                );
                lengthField = NewComp.getNumberTxtField("textFieldNumBFrame");
                lengthField.setValue(toMmOrIn(
                    PathPlanning.getMetricSystem(),
                    maxToolLength)
                );
                lengthField.getDocument().addDocumentListener(
                    new DocumentListener() 
                    {
                        public void insertUpdate(DocumentEvent event) 
                        {
                            if(changed.lastIndexOf(Id.MAXIMUM_TOOL_LENGTH) == -1)
                                changed.add(Id.MAXIMUM_TOOL_LENGTH);
                        }
                        public void removeUpdate(DocumentEvent event) 
                        {
                            if(changed.lastIndexOf(Id.MAXIMUM_TOOL_LENGTH) == -1)
                                changed.add(Id.MAXIMUM_TOOL_LENGTH);
                        }
                        public void changedUpdate(DocumentEvent event){}
                    }
                );
                panel_aux1.add(diameterField,new GridBagConstraints());
                panel_aux2.add(lengthField,new GridBagConstraints());
                panel.add(diameterLabel);
                panel.add(panel_aux1);
                panel.add(lenghtLabel);
                panel.add(panel_aux2);
                return panel;
            default:
                return null;
        }
    }    
    public String getDataCategoryID()
    {
        return "toolHolder";
    }
    public String getValue(Id dataID)
    {
        return null;
    }
    public boolean setCommandValue(int id,int value)
    {
        return initData(id,value);
    }
    public boolean setCommandValue(int id,long value)
    {
        return initData(id,(int)value);
    }
    public boolean setDataValue(int id,int value)
    {
        return initData(id,value);
    }
    public boolean setDataValue(int id,long value)
    {
        return initData(id,(int)value);
    }
    public void applyChange()
    {
        while( changed.isEmpty() == false)
        {
            long aux = 0;
            switch(changed.get(0))
            {
                case MAXIMUM_TOOL_DIAMETER:
                    aux =toNm(
                        PathPlanning.getMetricSystem(),
                        ((Number)diameterField.getValue()).floatValue()
                    );
                    if((aux < 0)||(aux > MAX_UINT_32))
                    {
                        diameterField.setValue(
                            toMmOrIn(
                                PathPlanning.getMetricSystem(),
                                maxToolDiameter
                            )
                        );
                        diameterField.requestFocus();
                        return;
                    }else
                    {
                        maxToolDiameter = aux;
                        InfoManager.addDataValue(
                            MAXIMUM_TOOL_DIAMETER,
                            maxToolDiameter
                        );
                    }
                    break;
                case MAXIMUM_TOOL_LENGTH:
                    aux = toNm(
                        PathPlanning.getMetricSystem(),
                        ((Number)lengthField.getValue()).floatValue());
                    if((aux < 0)||(aux > MAX_UINT_32))
                    {
                        lengthField.setValue(
                            toMmOrIn(
                                PathPlanning.getMetricSystem(),
                                maxToolLength
                            )
                        );
                        lengthField.requestFocus();
                        return;
                    }else
                    {
                        maxToolLength = aux;
                        InfoManager.addDataValue(
                            MAXIMUM_TOOL_LENGTH, 
                            maxToolLength
                        );
                    }
                    break;
                case OTHER:
                    break;
                default:
                    break;
            }
            changed.remove(0);
        }
    }
    public void cancelChange()
    {
        while( changed.isEmpty() == false)
        {
            switch(changed.get(0))
            {
                case MAXIMUM_TOOL_DIAMETER:
                    diameterField.setValue(toMmOrIn(
                        PathPlanning.getMetricSystem(),
                        maxToolDiameter)
                    );
                    break;
                case MAXIMUM_TOOL_LENGTH:
                    lengthField.setValue(toMmOrIn(
                        PathPlanning.getMetricSystem(),
                        maxToolLength)
                    );
                    break;
                case OTHER:
                    break;
                default:
                    break;
            }
        }
        changed.clear();
    }
    static
    {
        maxToolDiameter = new Long(0);
        maxToolLength = new Long(0);
    }
    private static Long maxToolDiameter;
    private static JFormattedTextField diameterField;
    private static Long maxToolLength;
    private static JFormattedTextField lengthField;
}
